<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="">
      <el-form-item label="广告名" prop="advertisingName">
        <el-input v-model="queryParams.advertisingName" placeholder="请输入广告名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="广告图片" prop="advertisingImagePath">
        <el-input
          v-model="queryParams.advertisingImagePath"
          placeholder="请输入广告图片路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="广告跳转链接" prop="advertisingUrl">
        <el-input
          v-model="queryParams.advertisingUrl"
          placeholder="请输入广告跳转链接"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="广告发布者" prop="pulisher">
        <el-input v-model="queryParams.pulisher" placeholder="请输入广告发布者" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="广告发布时间" prop="releaseTime">
        <el-date-picker clearable v-model="queryParams.releaseTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择广告发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="广告结束时间" prop="endTime">
        <el-date-picker clearable v-model="queryParams.endTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择广告结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="删除标识" prop="isDel">
        <template slot="label">
          <span>
            删除标识
            <el-tooltip class="item" effect="dark" content="0-正常，1-停用" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
          </span>
        </template>
        <el-input v-model="queryParams.isDel" clearable placeholder="请输入删除标识" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="删除标志" prop="idDel">
        <el-input
          v-model="queryParams.idDel"
          placeholder="请输入删除标志"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段1" prop="ext1">
        <el-input
          v-model="queryParams.ext1"
          placeholder="请输入扩展字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段2" prop="ext2">
        <el-input
          v-model="queryParams.ext2"
          placeholder="请输入扩展字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段3" prop="ext3">
        <el-input
          v-model="queryParams.ext3"
          placeholder="请输入扩展字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段4" prop="ext4">
        <el-input
          v-model="queryParams.ext4"
          placeholder="请输入扩展字段4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段5" prop="ext5">
        <el-input
          v-model="queryParams.ext5"
          placeholder="请输入扩展字段5"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
    </el-form>

    <el-row :gutter="10" class="mb8" style="display: flex;">
      <el-col style="justify-content: left;" :span="20">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:advertising:add']">新增</el-button>
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:advertising:edit']">修改</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:advertising:remove']">删除</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:advertising:export']">导出</el-button>
      </el-col>
      <el-col style="justify-content: right;" class="text-right" v-show="showSearch" :span="2.5">
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :span="1.5"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="advertisingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="广告id" align="center" prop="advertisingId" />
      <el-table-column label="广告名" align="center" prop="advertisingName" />
      <el-table-column label="广告图片" align="center" prop="advertisingImagePath">
        <template slot-scope="scope">
          <p v-if="scope.row.advertisingImagePath == null">无图片</p>
          <image-preview v-if="scope.row.advertisingImagePath != null" :src="scope.row.advertisingImagePath" :width="50" :height="50" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="广告介绍" align="center" prop="advertisingIntro" /> -->
      <el-table-column label="广告跳转链接" align="center" prop="advertisingUrl" />
      <el-table-column label="广告发布者" align="center" prop="pulisher" />
      <el-table-column label="广告发布时间" align="center" prop="releaseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="广告结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="删除标志" align="center" prop="idDel" />
      <!-- <el-table-column label="扩展字段1" align="center" prop="ext1" />
      <el-table-column label="扩展字段2" align="center" prop="ext2" />
      <el-table-column label="扩展字段3" align="center" prop="ext3" />
      <el-table-column label="扩展字段4" align="center" prop="ext4" />
      <el-table-column label="扩展字段5" align="center" prop="ext5" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['monitor:job:query']">详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改广告管理对话框 -->
    <el-dialog :title="title" :visible.sync="openEdit" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="广告id" prop="advertisingId">
          <el-input v-model="form.advertisingId" placeholder="广告id自动生成，不必填写" readonly/>
        </el-form-item>
        <el-form-item label="广告名" prop="advertisingName">
          <el-input v-model="form.advertisingName" placeholder="请输入广告名" />
        </el-form-item>
        <el-form-item label="广告图片路径" prop="advertisingImagePath">
          <image-advertisingImg-upload v-model="form.advertisingImagePath" />
        </el-form-item>
        <el-form-item label="广告介绍" prop="advertisingIntro">
          <editor v-model="form.advertisingIntro" :min-height="192" />
        </el-form-item>
        <el-form-item label="广告跳转链接" prop="advertisingUrl">
          <el-input v-model="form.advertisingUrl" placeholder="请输入广告跳转链接" />
        </el-form-item>
        <el-form-item label="广告发布者" prop="pulisher">
          <el-input v-model="form.pulisher" placeholder="请输入广告发布者" />
        </el-form-item>
        <el-form-item label="广告发布时间" prop="releaseTime">
          <el-date-picker clearable v-model="form.releaseTime"  type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择广告发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="广告结束时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime"  type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择广告结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志" prop="idDel">
          <template slot="label">
            <span>
              删除标识
              <el-tooltip class="item" effect="dark" content="0-正常，1-停用" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.idDel" placeholder="请输入删除标识（0正常，1停用）" />
        </el-form-item>
        <!-- <el-form-item label="扩展字段1" prop="ext1">
          <el-input v-model="form.ext1" placeholder="请输入扩展字段1" />
        </el-form-item>
        <el-form-item label="扩展字段2" prop="ext2">
          <el-input v-model="form.ext2" placeholder="请输入扩展字段2" />
        </el-form-item>
        <el-form-item label="扩展字段3" prop="ext3">
          <el-input v-model="form.ext3" placeholder="请输入扩展字段3" />
        </el-form-item>
        <el-form-item label="扩展字段4" prop="ext4">
          <el-input v-model="form.ext4" placeholder="请输入扩展字段4" />
        </el-form-item>
        <el-form-item label="扩展字段5" prop="ext5">
          <el-input v-model="form.ext5" placeholder="请输入扩展字段5" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="openView" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="广告id" prop="advertisingId">
          <el-input v-model="form.advertisingId" placeholder="广告id自动生成，不必填写" readonly/>
        </el-form-item>
        <el-form-item label="广告名" prop="advertisingName">
          <el-input v-model="form.advertisingName" placeholder="未填入广告名" readonly/>
        </el-form-item>
        <el-form-item label="广告图片路径" prop="advertisingImagePath">
          <template slot-scope="scope">
            <p v-if="form.advertisingImagePath == null">无图片</p>
            <image-preview v-if="form.advertisingImagePath != null" :src="form.advertisingImagePath" :width="50" :height="50" />
          </template>
        </el-form-item>
        <el-form-item label="广告介绍" prop="advertisingIntro">
          <editor v-model="form.advertisingIntro" :min-height="192" readonly />
        </el-form-item>
        <el-form-item label="广告跳转链接" prop="advertisingUrl">
          <el-input v-model="form.advertisingUrl" placeholder="未填入广告跳转链接" readonly/>
        </el-form-item>
        <el-form-item label="广告发布者" prop="pulisher">
          <el-input v-model="form.pulisher" placeholder="未填入广告发布者" readonly/>
        </el-form-item>
        <el-form-item label="广告发布时间" prop="releaseTime">
          <el-date-picker clearable v-model="form.releaseTime"  type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="未填入广告发布时间" readonly>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="广告结束时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime"  type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="未填入广告结束时间" readonly>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志" prop="idDel">
          <template slot="label">
            <span>
              删除标识
              <el-tooltip class="item" effect="dark" content="0-正常，1-停用" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </template>
          <el-input v-model="form.idDel" placeholder="未填入删除标识（0-正常，1-停用）" readonly/>
        </el-form-item>
        <!-- <el-form-item label="扩展字段1" prop="ext1">
          <el-input v-model="form.ext1" placeholder="请输入扩展字段1" />
        </el-form-item>
        <el-form-item label="扩展字段2" prop="ext2">
          <el-input v-model="form.ext2" placeholder="请输入扩展字段2" />
        </el-form-item>
        <el-form-item label="扩展字段3" prop="ext3">
          <el-input v-model="form.ext3" placeholder="请输入扩展字段3" />
        </el-form-item>
        <el-form-item label="扩展字段4" prop="ext4">
          <el-input v-model="form.ext4" placeholder="请输入扩展字段4" />
        </el-form-item>
        <el-form-item label="扩展字段5" prop="ext5">
          <el-input v-model="form.ext5" placeholder="请输入扩展字段5" />
        </el-form-item> -->
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listAdvertising, getAdvertising, delAdvertising, addAdvertising, updateAdvertising } from "@/api/system/advertising";

export default {
  name: "Advertising",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告管理表格数据
      advertisingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openEdit: false,
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        advertisingName: null,
        advertisingImagePath: null,
        advertisingIntro: null,
        advertisingUrl: null,
        pulisher: null,
        releaseTime: null,
        endTime: null,
        idDel: null,
        ext1: null,
        ext2: null,
        ext3: null,
        ext4: null,
        ext5: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询广告管理列表 */
    getList() {
      this.loading = true;
      listAdvertising(this.queryParams).then(response => {
        this.advertisingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.openView = true;
      this.form = row;
      this.title = "广告管理详细（如需修改，请通过修改按钮进行修改）";
    },
    // 取消按钮
    cancel() {
      this.openEdit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        advertisingId: null,
        advertisingName: null,
        advertisingImagePath: null,
        advertisingIntro: null,
        advertisingUrl: null,
        pulisher: null,
        releaseTime: null,
        endTime: null,
        idDel: null,
        createTime: null,
        updateTime: null,
        ext1: null,
        ext2: null,
        ext3: null,
        ext4: null,
        ext5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.advertisingId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openEdit = true;
      this.title = "添加广告管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const advertisingId = row.advertisingId || this.ids
      getAdvertising(advertisingId).then(response => {
        this.form = response.data;
        this.openEdit = true;
        this.title = "修改广告管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.advertisingId != null) {
            updateAdvertising(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openEdit = false;
              this.getList();
            });
          } else {
            addAdvertising(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openEdit = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const advertisingIds = row.advertisingId || this.ids;
      this.$modal.confirm('是否确认删除广告管理编号为"' + advertisingIds + '"的数据项？').then(function () {
        return delAdvertising(advertisingIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/advertising/export', {
        ...this.queryParams
      }, `advertising_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
